package com.chuangke.quartz.service.impl;

import java.util.List;

import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chuangke.quartz.dao.SysJobLogDaoService;
import com.chuangke.quartz.entity.SysJobLog;
import com.chuangke.quartz.service.SysJobLogService;

import cn.hutool.core.util.StrUtil;

/**
 * 定时任务调度日志信息 服务层
 * 
 */
@Service
public class SysJobLogServiceImpl implements SysJobLogService {

	@Autowired
	private SysJobLogDaoService sysJobLogDaoService;

	/**
	 * 获取quartz调度器日志的计划任务
	 * 
	 * @param jobLog 调度日志信息
	 * @return 调度任务日志集合
	 */
	@Override
	public List<SysJobLog> list(SysJobLog jobLog) {
		QueryWrapper<SysJobLog> queryWrapper = new QueryWrapper<>();
		if (!StrUtil.isBlank(jobLog.getJobGroup())) {
			queryWrapper.lambda().like(SysJobLog::getJobGroup, jobLog.getJobGroup());
		}
		if (!StrUtil.isBlank(jobLog.getId())) {
			queryWrapper.lambda().like(SysJobLog::getId, jobLog.getId());
		}
		if (!StrUtil.isBlank(jobLog.getName())) {
			queryWrapper.lambda().like(SysJobLog::getName, jobLog.getName());
		}
		if (!StrUtil.isBlank(jobLog.getStatus())) {
			queryWrapper.lambda().like(SysJobLog::getStatus, jobLog.getStatus());
		}
		if (!StrUtil.isBlank(jobLog.getInvokeTarget())) {
			queryWrapper.lambda().like(SysJobLog::getInvokeTarget, jobLog.getInvokeTarget());
		}
		if (!ObjectUtils.isEmpty(jobLog.getStartTime())) {
			queryWrapper.lambda().ge(SysJobLog::getCreatedAt, jobLog.getStartTime());
		}
		if (!ObjectUtils.isEmpty(jobLog.getStopTime())) {
			queryWrapper.lambda().le(SysJobLog::getCreatedAt, jobLog.getStopTime());
		}

		queryWrapper.lambda().orderByDesc(SysJobLog::getCreatedAt) ;
		return sysJobLogDaoService.list(queryWrapper);
	}

	/**
	 * 通过调度任务日志ID查询调度信息
	 * 
	 * @param jobLogId 调度任务日志ID
	 * @return 调度任务日志对象信息
	 */
	@Override
	public SysJobLog findById(String jobLogId) {
		return sysJobLogDaoService.getById(jobLogId);
	}

	/**
	 * 新增任务日志
	 * 
	 * @param jobLog 调度日志信息
	 */
	@Override
	public void save(SysJobLog jobLog) {
		sysJobLogDaoService.save(jobLog);
	}

	/**
	 * 批量删除调度日志信息
	 * 
	 * @param logIds 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public void deleteByIds(List<String> logIds) {
		sysJobLogDaoService.removeBatchByIds(logIds);
	}

	/**
	 * 删除任务日志
	 * 
	 * @param jobId 调度日志ID
	 */
	@Override
	public void deleteById(String jobId) {
		sysJobLogDaoService.removeById(jobId);
	}

	/**
	 * 清空任务日志
	 */
	@Override
	public void cleanJobLog() {
		sysJobLogDaoService.remove(new QueryWrapper<>());
	}
}
